package bupt.party.online.action;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import bupt.party.online.model.LearnRecord;
import bupt.party.online.model.Student;
import bupt.party.online.service.ILearnRecordService;
import bupt.party.online.service.IStudentService;
import bupt.party.online.util.BuptPartyConfig;
import bupt.party.online.util.SessionConstants;

public class LogoutAction extends ActionSupport implements ServletRequestAware, ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private HttpServletRequest request;
    private HttpServletResponse response; 
    private IStudentService studentService;
    private ILearnRecordService learnRecordService;
	
	
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	

	public IStudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	public ILearnRecordService getLearnRecordService() {
		return learnRecordService;
	}

	public void setLearnRecordService(ILearnRecordService learnRecordService) {
		this.learnRecordService = learnRecordService;
	}
	
	/* 学生退出*/
	public String logout(){
		HttpSession session = request.getSession();
		if(session.getAttribute(SessionConstants.SESSION_USER)==null){
			return "loginUI";
		}
		
		
		//退出的时候，要保存本次的学习时间长度
		Timestamp logoutTime = new Timestamp(System.currentTimeMillis());
		Student session_student = (Student) session.getAttribute(SessionConstants.SESSION_USER);
		Student student = studentService.findStudentByStudentNO(session_student.getStudentNo());
		Timestamp loginTime = student.getStudentLatestLoginTime();
		
		double studyTime = BuptPartyConfig.timeDifference(loginTime,logoutTime);
		
		LearnRecord learnRecord = new LearnRecord();
		learnRecord.setStudentNo(student.getStudentNo());
		learnRecord.setVisiteDate(student.getStudentLatestLoginTime());
		learnRecord.setVisiteIp(student.getStudentLatestLoginIp());
		learnRecord.setStudyTime(studyTime);
		
		learnRecordService.saveOrUpdate(learnRecord);
		//退出的时候，将本次的学习时长studyTime 加到 student的总学习时间上
		double newLearnTimeSum = studyTime+student.getStudentLearnTime();
		student.setStudentLearnTime(newLearnTimeSum);
		studentService.saveOrUpdateStudent(student);
		
		
		
		String studentNo = student.getStudentNo();
		Map appMap = ServletActionContext.getContext().getApplication();
		Map sessionMap = (Map)appMap.get("sessionMap");
		if(sessionMap==null){
			sessionMap = new HashMap();
			appMap.put("sessionMap", sessionMap);
		}
		sessionMap.remove(studentNo);
		
		
		
		
		//最终，将session的用户信息清空，置为null
		session.setAttribute(SessionConstants.SESSION_USER, null);
		session.setAttribute(SessionConstants.LEVEL, null);
		return "logout";
	}
	
	
	/* 管理员退出*/
	public String adminLogout(){
		HttpSession session = request.getSession();
		session.setAttribute(SessionConstants.SESSION_USER, null);
		session.setAttribute(SessionConstants.LEVEL, null);
		return "adminLogout";
	}
	
}
